<?php

defined('BASEPATH') or die('forbidden');

/**
 * 模型基类
 * @author  Karson
 * @name    model.php
 * @since   2012-8-15 11:37:04
 */
class Model {

    var $parent_name;

    function Model() {
        $this->assign_libraries((method_exists($this, '__get') OR method_exists($this, '__set')) ? FALSE : TRUE);
        $this->parent_name = ucfirst(get_class($this));
    }

    function assign_libraries($use_reference = TRUE) {
        $framework = & framework::instance()->controller;
        foreach (array_keys(get_object_vars($framework)) as $key) {
            if (!isset($this->$key) AND $key != $this->parent_name) {
                if ($use_reference == TRUE) {
                    $this->$key = NULL;
                    $this->$key = & $framework->$key;
                } else {
                    $this->$key = $framework->$key;
                }
            }
        }
        $framework->models[] = &$this;
    }

    function __call($name, $args) {
        throw new Exception("未找到请求的模型方法[{$name}],参数[" . implode(',', $args) . "]");
    }

}
